package com.backend.modules.system.convert;

import com.backend.common.enums.DeptType;
import com.backend.modules.system.entity.SysDepartment;
import com.backend.modules.system.vo.DepartmentVO;
import com.backend.modules.system.vo.TeacherClassVO;

import java.util.List;
import java.util.stream.Collectors;

public class DepartmentConvert {
    
    public static DepartmentVO convert(SysDepartment entity) {
        if (entity == null) {
            return null;
        }
        
        DepartmentVO vo = new DepartmentVO();
        vo.setId(entity.getId());
        vo.setName(entity.getName());
        vo.setType(entity.getType());
        // 设置类型描述
        DeptType type = DeptType.valueOf(entity.getType());
        if (type != null) {
            vo.setTypeDesc(type.getDesc());
        }
        
        // 设置父级信息
        if (entity.getParent() != null) {
            vo.setParentId(entity.getParent().getId());
            vo.setParentName(entity.getParent().getName());
        }
        
        // 设置子部门
        if (entity.getChildren() != null && !entity.getChildren().isEmpty()) {
            vo.setChildren(convertList(entity.getChildren()));
        }
        
        return vo;
    }
    
    public static List<DepartmentVO> convertList(List<SysDepartment> list) {
        return list.stream()
                .map(DepartmentConvert::convert)
                .collect(Collectors.toList());
    }
    
    public static TeacherClassVO convertClass(SysDepartment entity) {
        if (entity == null) {
            return null;
        }
        
        TeacherClassVO vo = new TeacherClassVO();
        vo.setId(entity.getId());
        vo.setName(entity.getName());
        
        // 设置专业和学院信息
        if (entity.getParentId() != null) {
            SysDepartment major = entity.getParent();
            if (major != null) {
                vo.setMajorId(major.getId());
                vo.setMajorName(major.getName());
                
                if (major.getParentId() != null) {
                    SysDepartment college = major.getParent();
                    if (college != null) {
                        vo.setCollegeId(college.getId());
                        vo.setCollegeName(college.getName());
                    }
                }
            }
        }
        
        return vo;
    }
    
    public static List<TeacherClassVO> convertClassList(List<SysDepartment> list) {
        return list.stream()
                .map(DepartmentConvert::convertClass)
                .collect(Collectors.toList());
    }
} 